5
5
.
.
1
1
6
6
.
.
o
o
n
n
L
L
o
o
n
n
g
g
P
P
r
r
e
e
s
s
s
s
G
G
e
e
s
s
t
t
u
u
r
r
e
e
I
I
n
n
f
f
o
o
.onLongPressGesture Modifier allows View to react when User holds finger on top of it.
Provided Closure is called when event finishes (when minimumDuration expires).
Alternative is to use .gesture(LongPressGesture) whose Closure accepts additional Boolean parameter.
Compared to .gesture this Closure has no parameter.
Gestures tutorial contains additional explanations.
Syntax
.onLongPressGesture(minimumDuration: 1, maximumDistance: 1) { print("Event ended") }
.onLongPressGesture { print("Event ended") }
Parameters
NAME
DESCRIPTION
minimumDuration
The duration in seconds that must elapse before the gesture ends
maximumDistance
The maximum distance the event can move before the gesture fails
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In the below example circle changes color when you Long Press on it.
TapGesture().onEnded()
struct ContentView: View {
@State var color = true
var body: some View {
Circle()
.fill(color ? Color.green : Color.red)
.frame(width: 50, height: 50)
.onLongPressGesture(minimumDuration: 1, maximumDistance: 1) { //Trailing Closure has no
parameters
self.color.toggle()
}
}
}
Initial color Long Press to change color